//
//  HRChooseAddressViewController.m
//  haierapp
//
//  Created by Xia Zhiyong on 14-8-26.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRChooseAddressViewController.h"
#import "HRChooseAddressTableHead.h"
#import "HRTakeAddressTableViewCell.h"
#import "HREditAddressViewController.h"
#import "HRCreateNewAddressViewController.h"

@interface HRChooseAddressViewController (){
    NSMutableArray *arrayTable ;
    NSInteger currentIndex;
}

@end

@implementation HRChooseAddressViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       arrayTable = [[NSMutableArray alloc] init];
        currentIndex = -1;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (__CUR_IOS_VERSION >= __IPHONE_7_0)
    {
        self.edgesForExtendedLayout= UIRectEdgeNone;
    }
    self.navigationItem.title = @"选择收货地址";
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"新建" style:UIBarButtonItemStylePlain target:self action:@selector(createNewAddress)];
    [self.navigationItem setRightBarButtonItem:rightItem];
    
    [self loadData];

}

-(void)loadData{
    [self showCustomLoadingHud:kLoading inView:nil];
    [arrayTable removeAllObjects];
    [HRInterfaceManager getReceiveAddressList:^(BOOL isSucceed, NSString *message, id data) {
        [self hideCustomLoadingHud];
        
        if (isSucceed) {
            HRAddressList *model = (HRAddressList*)data;
            [arrayTable addObjectsFromArray:model.list];
            [_tableView reloadData];
            _viewBottom.hidden = NO;
        }
        [self hideHud];
        if (arrayTable==nil || [arrayTable count]==0) {
            //显示为空提示
            [self showHudByType:HUDTYPE_ADDRESS];
            _viewBottom.hidden=YES;
        }
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [arrayTable count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 35;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    HRChooseAddressTableHead *viewHead = [[HRChooseAddressTableHead alloc] init];
    if(currentIndex == section){
        viewHead.btnChoose.selected = YES;
    }
    viewHead.btnClicked = ^(int actionType){
        switch (actionType) {
            case 0:{
                currentIndex = section;
                [_tableView reloadData];
            }
                break;
            case 1:{
                HREditAddressViewController *viewC = [[HREditAddressViewController alloc] initWithNibName:@"HREditAddressViewController" bundle:nil];
                viewC.info = (HRAddressInfo*)[arrayTable objectAtIndex:section];
                viewC.addressChange = ^(){ //编辑页面地址信息变化后刷新列表
                    [self loadData];
                };
                [self.navigationController pushViewController:viewC animated:YES];
                break;
            }
                default:
                break;
        }
    };
    return viewHead;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"HRTakeAddressTableViewCell";
    HRTakeAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HRTakeAddressTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell configWithData:[arrayTable objectAtIndex:indexPath.section]];
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)actionChoose:(id)sender {
    if (currentIndex == -1) {
        [self showTextHUD:@"请选择收货地址"];
        return;
    }
    if (_passAddress) {
        _passAddress([arrayTable objectAtIndex:currentIndex]);
        [self.navigationController popViewControllerAnimated:YES];
    }
}
//新建地址
-(void)createNewAddress{
    HRCreateNewAddressViewController *viewC = [[HRCreateNewAddressViewController alloc] initWithNibName:@"HRCreateNewAddressViewController" bundle:nil];
    viewC.createAddressSucced = ^(){
        [self loadData];
    };
    [self.navController pushViewController:viewC animated:YES];
}

#pragma mark - HRProgress Delegate Methods
//创建新地址
-(void)tapHUDButtonAction:(id)button{
    [self createNewAddress];
}

@end
